
var map;
var layera, layerb, layerkec, layerkab, layerdes,  layer_bg, titik_tengah, layer_hgu, layer_jalan, layer_kebun;
var blok_kabupaten, layer_pertanian, layer_tol, layer_tubuh_air, layer_kereta_api, layer_gunung, layer_sungai;
var layer_rtrw;
var klik;
var id_map_kecamatan='5216934',
id_map_desa='1wsg6GLA4PUIAF16wVN7ssGT5bm4lIVVl_wA_TWo',
id_map_kabupaten='',
id_map_hgu='5216868',
id_map_jalan='5216879',
id_map_sungai='5280000',
id_map_kebun='5217543',
id_map_pertanian='5278081',
id_map_rtrw='5278096',
id_map_annotext='5279902',
id_map_anotasi_gunung='5279523',
id_map_batas_admin_line='5277784',
id_map_point_ibukota_kecamata='5277986',
id_map_kereta_api='5280006',
id_map_tubuh_air='5279532',
id_map_tol='5279533',
id_map_sarana='5280014';
var desa_klik=0, kec_klik=0, kab_klik=0, inited=0, jalan_klik=0, kebun_klik=0, hgu_klik=0, blok_kabupaten_klik=0;
var layer_pertanian_klik=0, layer_gunung_klik=0, layer_kereta_api_klik=0, layer_tol_klik=0, layer_sungai_klik=0;
$(document).ready(function(){
    $("#rc1").change(function(){
        if($(this).is(':checked')) {   
            $('#c1').attr('checked', 'checked');
            $('#c2').attr('checked', 'checked'); 
            $('#c3').attr('checked', 'checked'); 
        }
    })
          
    $("#administrasi_cek").change(function(){
        if($(this).is(':checked')) {  
            $('#desa_cek').attr('checked', true);
            $('#kecamatan_cek').attr('checked', true); 
            $('#kabupaten_cek').attr('checked', true);
            $('#jalan_cek').attr('checked', true);
            $('#sungai_cek').attr('checked', true);
            desa_klik=kec_klik=kab_klik=jalan_klik=1;
        }
        else{
            $('#desa_cek').attr('checked', false);
            $('#kecamatan_cek').attr('checked', false); 
            $('#kabupaten_cek').attr('checked', false); 
            $('#jalan_cek').attr('checked', false);
            $('#sungai_cek').attr('checked', false);
            desa_klik=kec_klik=kab_klik=jalan_klik=0;
        }
        draw_shp();
    })
    
    $("#desa_cek").change(function(){
        if($(this).is(':checked')) {
            if($('#kecamatan_cek').is(':checked') ){
                desa_klik=1;
            //layerdes.setMap(map);
            }
            else
            {
                $(this).attr('checked',false);
            }
        }
        else{
            desa_klik=0;
        //layerdes.setMap(null);
        }
    })
    
    $("#kecamatan_cek").change(function(){
        if($(this).is(':checked')) {
            kec_klik=1;
            draw_shp();
        }
        else{
            desa_klik=0;
            kec_klik=0;
            draw_shp();
        }
    })
    
    $("#kabupaten_cek").change(function(){
        if($(this).is(':checked')) {
            kab_klik=1;
        }
        else{
            kab_klik=0;
        }
    })
    
    $("#sungai_cek").change(function(){
        if($(this).is(":checked")){
            
        }
        else        {               
    }
    })
    
    $("#jalan_cek").change(function(){
        if($(this).is(":checked")){
            //layer_jalan.setMap(map);
            jalan_klik=1;
        }
        else{
            //layer_jalan.setMap(null);
            jalan_klik=0;
            
        }
        draw_shp();
    })
    
    $("#izin_kebun_cek").change(function(){
        if($(this).is(":checked")){
            kebun_klik=1;
        }
        else{
            kebun_klik=0;
        }
        draw_shp();
    })
    $("#hgu_cek").change(function(){
        if($(this).is(":checked"))
        {
            hgu_klik=1;
        }
        else{
            hgu_klik=0;
        }
        draw_shp();
    })
    $("#izin_cek").change(function(){
        if($(this).is(":checked"))
        {
            $("#izin_kebun_cek").attr("checked",true);
            $("#hgu_cek").attr("checked",true);
            hgu_klik=kebun_klik=1;
            draw_shp();
        }
        else
        {
            $("#izin_kebun_cek").attr("checked",false);
            $("#hgu_cek").attr("checked",false);    
            hgu_klik=kebun_klik=0;
            draw_shp();
        }
    })
    $("#tol_cek").change(function(){
        if($(this).is(":checked")){
            layer_tol_klik=1;
        }
        else
        {
            layer_tol_klik=0;
        }
        draw_shp();
    })
    $("#sungai_cek").change(function(){
        if($(this).is(":checked")){
            layer_sungai_klik=1;
        }
        else
        {
            layer_sungai_klik=0;
        }
        draw_shp();
    })
    $("#rtrw_cek").change(function(){
        if($(this).is(":checked")){
            
        }
        else
        {
        }
        draw_shp();
    })
    $("#anotasi_gunung_cek").change(function(){
        if($(this).is(":checked")){
            layer_gunung_klik=1;
        }
        else
        {
            layer_gunung_klik=0;
        }
        draw_shp();
    })
    $("#peta_kereta_api_cek").change(function(){
        if($(this).is(":checked")){
            layer_kereta_api_klik=1;
        }
        else
        {
            layer_kereta_api_klik=0;
        }
        draw_shp();
    })
    $("#peta_pertanian_cek").change(function(){
        if($(this).is(":checked")){
            layer_pertanian_klik=1;
        }
        else
        {
            layer_pertanian_klik=0;    
        }
        draw_shp();
    })
    
    
    
});

function initialize() {
    klik =0;
    //1.241358,116.332397
    var kaltim = new google.maps.LatLng(1.241358,116.332397);
    map = new google.maps.Map(document.getElementById('map_canvas'), {
        center: kaltim,
        zoom: 7,
        mapTypeControl: false,
        panControl: false,
        mapTypeControlOptions: {
            style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
        },
        zoomControl: true,
        zoomControlOptions: {
            style: google.maps.ZoomControlStyle.SMALL,
            position: google.maps.ControlPosition.LEFT_TOP
        },
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });
    $('#administrasi_cek').attr('checked',true);
    $('#kecamatan_cek').attr('checked', true); 
    $('#kabupaten_cek').attr('checked', true);
    layerkec  = new  google.maps.FusionTablesLayer(id_map_kecamatan);
    layer_hgu = new google.maps.FusionTablesLayer(id_map_hgu);
    layer_kebun = new google.maps.FusionTablesLayer(id_map_kebun);
    layer_jalan = new google.maps.FusionTablesLayer(id_map_jalan);
    blok_kabupaten = new google.maps.FusionTablesLayer(id_map_kecamatan);
    layer_pertanian = new google.maps.FusionTablesLayer(id_map_pertanian);
    layer_kereta_api = new google.maps.FusionTablesLayer(id_map_kereta_api);
    layer_gunung = new google.maps.FusionTablesLayer(id_map_anotasi_gunung);
    layer_sungai = new google.maps.FusionTablesLayer(id_map_sungai);
    layer_tol = new google.maps.FusionTablesLayer(id_map_tol);
    layerkec.setMap(map);
    inited=0;
    kec_klik=1;
}

function testklik(kecamatan){ //MOUSE KLIK
    klik=1;
    layera.setQuery("select geometry from 5100275 where KECAMATAN='"+kecamatan+"'");
    layera.set("styleId",2);
    //alert("point() ");
    gvizSelect("5098719",kecamatan);
    if(inited==0){
        layera.setMap(map);
        inited=1;
    }
}
function test(kec) //mouse over
{
    layera.setQuery("select geometry from 5100275 where KECAMATAN='"+kec+"'");
    layera.set("styleId",9);
    klik=0;
    if(inited==0){
        layera.setMap(map);
        inited=1;
    }
}
function test1() //mouse out
{
    if(klik==0){
        layera.setQuery("select geometry from 5100275");
        layera.set("styleId",0);
        if(inited==1){
            layera.setMap(null);
            inited=0;
        }
    }
}
function change_map_view(tipe)
{
    if(tipe==1)
        map.setMapTypeId(google.maps.MapTypeId.SATELLITE);
    else if(tipe==2)
        map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
    else if(tipe==3)
        map.setMapTypeId(google.maps.MapTypeId.HYBRID);
    else if(tipe==4)
        map.setMapTypeId(google.maps.MapTypeId.TERRAIN);
}
function dump(obj) {
    var out = '';
    for (var i in obj) {
        out += i + ": " + obj[i] + "\n";
    }
    alert(out);
    var pre = document.createElement('pre');
    pre.innerHTML = out;
    document.body.appendChild(pre)
}
var googlegvis = "http://www.google.com/fusiontables/gvizdata?tq=";
function gvizSelect(tid,kecamatan) {
    var queryText = encodeURIComponent("select geometry from "+tid+" where NAMA_KOTA='" + kecamatan+"'");
    queryText = googlegvis+queryText;
    try{
        var query = new google.visualization.Query(queryText);
        query.send(getData);
    }
    catch(err)
    {
        alert(err);
    }
}
function getData(response) {
    numRows = response.getDataTable().getNumberOfRows();
    if(numRows>0){
        var koordinat = response.getDataTable().getValue(0,0);
        koordinat=koordinat.replace("<Point>","");
        koordinat=koordinat.replace("</Point>","");
        koordinat=koordinat.replace("<coordinates>","");
        koordinat=koordinat.replace("</coordinates>","");
        var koordinatbaru = koordinat.split(",");
        if(koordinatbaru.length>1){
            var pointttt = new google.maps.LatLng(koordinatbaru[1], koordinatbaru[0]);
            map.setCenter(pointttt);
            map.setZoom(8);
        }
    }
}
function callbacknull()
{
    
}

function centangAdministrasi(b)
{
    
}
function draw_shp()
{
    //alert("reset map");
    layerkec.setMap(null);
    layer_jalan.setMap(null);
    layer_kebun.setMap(null);
    layer_hgu.setMap(null);
    layer_pertanian.setMap(null);
    blok_kabupaten.setMap(null);
    layer_kereta_api.setMap(null);
    layer_sungai.setMap(null);
    layer_tol.setMap(null);
    layer_gunung.setMap(null);
    if(kec_klik==1)
    {
        //alert("draw kecamatan");
        layerkec.setMap(map);
    }
    if(blok_kabupaten_klik==1)
    {
        blok_kabupaten_klik=0;
        blok_kabupaten.setMap(map);
    //  alert("wkwk");
    }
    if(layer_sungai_klik==1){
        //alert("sungaiiii");
        layer_sungai.setMap(map);
    }
    if(jalan_klik==1)
    {
        //alert("draw jalan");
        layer_jalan.setMap(map);
    }
    if(hgu_klik==1){
        //alert("draw hgu");
        layer_hgu.setMap(map);
    }
    if(kebun_klik==1)
    {
        //alert("draw kebun");
        layer_kebun.setMap(map);
    }
    if(layer_pertanian_klik==1)
    {
        layer_pertanian.setMap(map);
    }
    if(layer_kereta_api_klik==1){
        layer_kereta_api.setMap(map);
    }
    
    if(layer_tol_klik==1){
        
        layer_tol.setMap(map);
    //       alert("tolllllllllllllllllllll");
    }
    if(layer_gunung_klik==1){
        layer_gunung.setMap(map);
    }
}
function kabupaten_klik(nama_kabupaten)
{
    blok_kabupaten.setQuery("select geometry from "+ id_map_kecamatan +" where KAB_KOTA_1='"+nama_kabupaten+"'");
    blok_kabupaten.set("styleId",2);
    blok_kabupaten_klik=1;
    draw_shp();
}